package co.com.coomeva.adminEvaluacion.customConverters;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
//import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.DateTimeConverter;


//javax.faces.converter.DateTimeConverter

public class DateConverterCustom extends DateTimeConverter {

	private static String DATE_FORMAT = "dd-MM-yyyy";
    private static SimpleDateFormat formato;
    
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		// TODO Auto-generated method stub
		boolean bError = false;
		Date dateFecha = null;
		
		//System.out.println("ENTRA A getAsObject!!!");
		
		try {
		    
		    if (value != null && value.trim().length() > 0) {
		    	Calendar cal = Calendar.getInstance();
		    	
		    	String[] strFecha = value.split("-");
		    	String strDia = strFecha[0];
		    	String strMes = strFecha[1];
		    	String strAnno = strFecha[2];
		    	
		    	//dateFecha = new Date();
		    	
		    	cal.set(Calendar.DATE, Integer.parseInt(strDia));
		    	cal.set(Calendar.MONTH, Integer.parseInt(strMes) - 1);
		    	cal.set(Calendar.YEAR, Integer.parseInt(strAnno));
		    	
		    	//Tiempo
		    	cal.set(Calendar.HOUR_OF_DAY, 12);
		    	cal.set(Calendar.MINUTE, 00);
		    	cal.set(Calendar.SECOND, 00);
		    	cal.set(Calendar.MILLISECOND, 00);
		    	
		    	dateFecha = cal.getTime();
		    	
		    	//dateFecha.setDate(Integer.parseInt(strDia));
		    	//dateFecha.setMonth(Integer.parseInt(strMes) - 1);
		    	//dateFecha.setYear(Integer.parseInt(strAnno) - 1900);
		    	
		    	//Al medio dia :(
		    	//dateFecha.setHours(12);
		    	//dateFecha.setMinutes(00);
		    	//dateFecha.setSeconds(00);
		    	
		    	
		    }
		} catch (Exception e) {
			dateFecha = null;
			//throw new ConverterException();
		}
		
		if (dateFecha == null) {
			//throw new ConverterException();
		}
		
		
		
		return dateFecha;
	}

	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		String strValor;
		if (value == null) {
			strValor = null;
		} else {
			formato = new SimpleDateFormat(DATE_FORMAT);
			strValor = formato.format((Date) value);
		}

		//System.out.println("ENTRA A getAsString!!!");
		return strValor;
	}
	
	//

}
